<?php
namespace Admin\Controller;
use Think\Controller;
class BaseController extends Controller {
    public function _initialize(){
        session_start();
        if(!isset($_SESSION["admin_member"])){
            header("location:".U('Admin/Login/index'));
        }
        //查找角色名称
        $role_id=$_SESSION["role_id"];
        $M = M("admin_role");
        $admin_role=$M->where("id='$role_id'")->select();
        $admin_role=$admin_role[0]["role_name"];
        $_SESSION["admin_role"]=$admin_role;

        /*
         * 判断所属角色的权限
         */
        $sql="select * from (select * from y_admin_role INNER JOIN y_admin_access on id=role_id) a,y_admin_node b where a.node_id=b.id and role_name='$admin_role'";
        $M = M();
        $admin_access=$M->query($sql);
        $role_name=array();
        $role_access = [];
        foreach($admin_access as $k=>$v){
            if(in_array($v['role_name'],$role_name)){
                $key = array_search($v['role_name'],$role_name);            
                $role_access[$key]['id'] = $role_access[$key]['id'].','.$v['id'];
                $role_access[$key]['role'] = $v['role_name'];
            }else{
                $role_name[] = $v['role_name'];
                $key = array_search($v['role_name'],$role_name);
                $role_access[$key]['id'] = $v['id'];
                $role_access[$key]['role'] = $v['role_name'];
            }
        }
        $id=$role_access["0"]["id"];
        $id=explode(",",$id);
        $_SESSION["node_id"]=$id;
        //节点
        $M = M("admin_node");
        $node=$M->where("parent_id='0'")->select();
        $this->assign("node",$node);
        $this->assign("id",$id);
        $_SESSION['access']=$node;
        // dump($_SESSION['access']);
        // dump($_SESSION['node_id']);
    }
}